<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form ref="ruleForm" :model="addForm" :rules="addRules" label-width="80px" class="demo-ruleForm" style="width:320px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="addForm.name" />
      </el-form-item>
      <el-form-item label="启用">
        <el-switch
          v-model="addForm.onOff"
          change="handleChange"
        />
      </el-form-item>
      <el-form-item label="角色描述" prop="describe">
        <el-input
          v-model="addForm.describe"
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
        />
      </el-form-item>
      <el-form-item style="text-align:center;margin-left:-80px;">
        <el-button type="primary" size="small" @click.native="onSubmit">确认</el-button>
        <el-button size="small" @click.native="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addRoleApi } from '@/api/role'

export default {
  name: 'AddRole',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '新增角色',
      addForm: {
        name: '',
        onOff: true,
        describe: ''
      },
      addRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        describe: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleChange(value) {
      this.onOff = value
    },
    handleClose() {
      this.$refs.ruleForm.resetFields()
      this.$emit('update:dialogVisible', false)
    },
    async onSubmit() {
      await this.$refs.ruleForm.validate()
      await addRoleApi({ name: this.addForm.name, description: this.addForm.describe, state: this.addForm.onOff ? 1 : 0 })
      this.$message.success('添加成功~')
      this.handleClose()
      this.$emit('updateRoleList')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
